<template>
  <div class="cart-sku" ref="target">
    <div class="attrs" @click="toggle">
      <span class="ellipsis">{{ attrsText }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="layer" v-show="isShow">
      <template v-if="goods">
        <GoodsSku @change="changeSku" :skuId="skuId" :goods="goods" />
        <XtxButton
          @click="submit"
          type="primary"
          size="mini"
          style="margin-left: 60px;"
        >
          确认
        </XtxButton>
      </template>
      <div v-else class="loading"></div>
    </div>
  </div>
</template>

<script setup>
import GoodsSku from '@/views/goods/components/goods-sku.vue'

const { getSpecsAndSkus } = require('@/api/cart')
const { ref } = require('@vue/reactivity')
const { onClickOutside } = require('@vueuse/core')
const props = defineProps({
  attrsText: {
    type: String,
    default: '',
  },
  skuId: {
    type: String,
    default: '',
  },
})

const isShow = ref(false)
const goods = ref(null)
const show = async () => {
  isShow.value = true
  const { result } = await getSpecsAndSkus(props.skuId)
  goods.value = result
}
const hide = () => {
  isShow.value = false
  goods.value = null
}
const toggle = () => {
  isShow.value ? hide() : show()
}
const target = ref(null)
// 点击了 target 元素之外的内容触发
onClickOutside(target, () => {
  hide()
})

// 选择SKU时候触发
// 记录选择的 sku
const currSku = ref(null)
const changeSku = (sku) => {
  currSku.value = sku
}

const emit = defineEmits(['change'])

// 点击确认的时候，提交sku信息给购物车组件
const submit = () => {
  // 给购物车组件数据的前提：有sku信息，sku信息完整，sku中的skuId不能现有props.skuId一样
  if (currSku?.value?.skuId && currSku.value.skuId !== props.skuId) {
    emit('change', currSku.value)
    hide()
  }
}
</script>

<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display: inline-block;
  .attrs {
    line-height: 24px;
    display: flex;
    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }
    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }
  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor, 50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;
    &::before {
      content: '';
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(0.8, 1) rotate(45deg);
    }
    .loading {
      height: 224px;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
